package cn.danger.com.fragment.user;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.assist.ImageSize;

import java.util.Map;

import cn.danger.com.activity.CommonActivity;
import cn.danger.com.activity.MainActivity;
import cn.danger.com.activity.R;
import cn.danger.com.fragment.BaseFragment;
import cn.danger.com.presenter.MemoPresenter;
import cn.danger.com.presenter.MemoPresenterImpl;
import cn.danger.com.presenter.SchedulePresenter;
import cn.danger.com.presenter.SchedulePresenterImpl;
import cn.danger.com.presenter.UserPresenter;
import cn.danger.com.presenter.UserPresenterImpl;
import cn.danger.com.util.FileCacheService;
import cn.danger.com.util.ImageLoaderUtil;
import cn.danger.com.view.CommonView;
import cn.danger.com.widget.SwitchButton;

/**
 * Created by 俊 on 2016/12/26.
 */
public class UserFragment extends BaseFragment implements View.OnClickListener, SwitchButton.OnCheckedChangeListener, CommonView {

    private View view;
    private MainActivity activity;
    private ImageView user_photo_imageView;
    private TextView nick_name_textView;
    private MyReceiver receiver;
    private SwitchButton show_finished_event_switch;
    private RelativeLayout work_color_layout;
    private RelativeLayout route_color_layout;
    private RelativeLayout life_color_layout;
    private RelativeLayout synchronous_layout;
    private RelativeLayout about_us_layout;
    private UserPresenter presenter;
    private ProgressDialog dialog;
    private MemoPresenter memoPresenter;
    private SchedulePresenter schedulePresenter;
    private Handler handler;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_user, container, false);

        bindView();
        bindEvent();

        return view;
    }

    @Override
    public void bindView() {
        activity = (MainActivity) getActivity();
        user_photo_imageView = (ImageView) view.findViewById(R.id.user_photo_imageView);
        nick_name_textView = (TextView) view.findViewById(R.id.nick_name_textView);
        show_finished_event_switch = (SwitchButton) view.findViewById(R.id.show_finished_event_switch);
        work_color_layout = (RelativeLayout) view.findViewById(R.id.work_color_layout);
        route_color_layout = (RelativeLayout) view.findViewById(R.id.route_color_layout);
        life_color_layout = (RelativeLayout) view.findViewById(R.id.life_color_layout);
        synchronous_layout = (RelativeLayout) view.findViewById(R.id.synchronous_layout);
        about_us_layout = (RelativeLayout) view.findViewById(R.id.about_us_layout);

        FileCacheService service = new FileCacheService(activity);
        Map<String, String> map = (Map<String, String>) service.getSharePreference("userInfo");
        if (map.get("photo") != null) {
            new ImageLoaderUtil(new ImageSize(60, 60), user_photo_imageView, map.get("photo"), activity).load();
        } else {
            user_photo_imageView.setImageResource(R.mipmap.default_photo);
        }
        if (map.get("nick_name") != null) {
            nick_name_textView.setText(map.get("nick_name"));
        } else if (map.get("nick_name") == null && map.get("phone") != null) {
            nick_name_textView.setText(map.get("phone"));
        } else {
            nick_name_textView.setText("登录/注册");
        }
        Map<String, Object> setting_map = (Map<String, Object>) service.getSharePreference("setting");
        if ((boolean) setting_map.get("show_finished_event") == true) {
            show_finished_event_switch.setChecked(true);
        } else {
            show_finished_event_switch.setChecked(false);
        }

        receiver = new MyReceiver();
        IntentFilter filter = new IntentFilter("update_user");
        activity.registerReceiver(receiver, filter);

        dialog = new ProgressDialog(activity, ProgressDialog.THEME_HOLO_LIGHT);
        dialog.setMessage("正在执行...");
        dialog.setCanceledOnTouchOutside(false);

        presenter = new UserPresenterImpl(activity);
        memoPresenter = new MemoPresenterImpl(activity, this);
        schedulePresenter = new SchedulePresenterImpl(activity, this);
    }

    @Override
    public void bindEvent() {
        user_photo_imageView.setOnClickListener(this);
        show_finished_event_switch.setOnCheckedChangeListener(this);
        work_color_layout.setOnClickListener(this);
        route_color_layout.setOnClickListener(this);
        life_color_layout.setOnClickListener(this);
        synchronous_layout.setOnClickListener(this);
        about_us_layout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_photo_imageView:
                FileCacheService service = new FileCacheService(activity);
                Map<String, String> map = (Map<String, String>) service.getSharePreference("userInfo");
                Intent intent = new Intent(getActivity(), CommonActivity.class);
                Log.i("danger", "phone------->>>" + map.get("phone"));
                Log.i("danger", "password------>>>" + map.get("password"));
                if (map.get("phone") != null && map.get("password") != null) {
                    intent.putExtra("target", "userInfo");
                } else {
                    intent.putExtra("target", "login");
                }
                startActivity(intent);
                break;
            case R.id.work_color_layout:
                Intent work_intent = new Intent(getActivity(), CommonActivity.class);
                work_intent.putExtra("target", "color");
                work_intent.putExtra("type", "work_color");
                startActivity(work_intent);
                break;
            case R.id.route_color_layout:
                Intent route_intent = new Intent(getActivity(), CommonActivity.class);
                route_intent.putExtra("target", "color");
                route_intent.putExtra("type", "route_color");
                startActivity(route_intent);
                break;
            case R.id.life_color_layout:
                Intent life_intent = new Intent(getActivity(), CommonActivity.class);
                life_intent.putExtra("target", "color");
                life_intent.putExtra("type", "life_color");
                startActivity(life_intent);
                break;
            case R.id.synchronous_layout:
                memoPresenter.synchronousMemo();
                schedulePresenter.synchronousSchedule();
                break;
            case R.id.about_us_layout:
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        switch (view.getId()) {
            case R.id.show_finished_event_switch:
                presenter.showFinishedEvent();
                handler.sendEmptyMessage(1);
                break;
            default:
                break;
        }
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.dismiss();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
    }

    private class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            FileCacheService service = new FileCacheService(activity);
            Map<String, String> map = (Map<String, String>) service.getSharePreference("userInfo");
            String update = intent.getStringExtra("update");
            if (update.equals("photo")) {
                new ImageLoaderUtil(new ImageSize(60, 60), user_photo_imageView, map.get("photo"), activity).load();
            } else if (update.equals("nick_name")) {
                nick_name_textView.setText(map.get("nick_name"));
            } else if (update.equals("all")) {
                new ImageLoaderUtil(new ImageSize(60, 60), user_photo_imageView, map.get("photo"), activity).load();
                if (map.get("nick_name") != null) {
                    nick_name_textView.setText(map.get("nick_name"));
                } else if (map.get("nick_name") == null && map.get("phone") != null) {
                    nick_name_textView.setText(map.get("phone"));
                }
            } else if (update.equals("logout")) {
                user_photo_imageView.setImageResource(R.mipmap.default_photo);
                nick_name_textView.setText("登录/注册");
            }
        }
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (receiver != null) {
            activity.unregisterReceiver(receiver);
        }
    }
}